﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace HobSoft.SoccerBet2014.Domain.Models
{
    using Core;
    using KeyHelper;
    [Serializable]
    [DataContract]
    public class Odds : IModel
    {
        public Odds(string oddsId, MarketType type)
        {
            Type = type;
            OddsID = oddsId;
            IsAvailable = true;
            Prices = new ObservableCollection<OddsPrice>();
        }

        public Odds()
        {
            Prices = new ObservableCollection<OddsPrice>();
            IsAvailable = true;
        }

        [PrimaryKey]
        [DataMember(IsRequired = true)]
        public string Key
        {
            get { return KeyGenerateHelper.Create<Odds>(this); }
            set { ; }
        }

        private byte _bmId;
        [Key]
        [Required]
        [DataMember(IsRequired = true)]
        public byte BmID
        {
            get { return _bmId; }
            set { _bmId = value; }
        }

        private string _oddsId;
        [Key]
        [Required]
        [DataMember(IsRequired = true)]
        [MaxLength(50)]
        [MinLength(1)]
        public string OddsID
        {
            get { return _oddsId; }
            set { _oddsId = value; }
        }

        private string _gameId;
        [Required]
        [MaxLength(50)]
        [DataMember(IsRequired = true)]
        public string GameID
        {
            get { return _gameId; }
            set { _gameId = value; }
        }

        private decimal _handicap;
        [Required]
        [DataMember(IsRequired = true)]
        public decimal Handicap
        {
            get { return _handicap; }
            set { _handicap = value; }
        }

        private MarketType _type;
        [Required]
        [DataMember(IsRequired = true)]
        public MarketType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        private ObservableCollection<OddsPrice> _price;
        [DataMember(IsRequired = true)]
        public ObservableCollection<OddsPrice> Prices
        {
            get { return _price; }
            set
            {
                if (_price == value) return;
                _price = value;
                if (_price != null)
                {
                    _price.CollectionChanged += Price_CollectionChanged;
                    foreach (var p in _price)
                    {
                        p.Odds = this;
                    }
                }
            }
        }

        private void Price_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
            {
                foreach (OddsPrice item in e.NewItems)
                {
                    if (item.Odds != this)
                        item.Odds = this;
                }
                if (e.OldItems != null)
                {
                    foreach (OddsPrice item in e.OldItems)
                    {
                        item.Odds = null;
                    }
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                if (e.OldItems != null)
                {
                    foreach (OddsPrice item in e.OldItems)
                    {
                        item.Odds = null;
                    }
                }
            }
        }

        private DateTime _lastUpdateTime;
        [DataMember]
        public DateTime LastUpdateTime
        {
            get { return _lastUpdateTime; }
            set { _lastUpdateTime = value; }
        }

        private string _lastUpdateAccount;
        [DataMember]
        public string LastUpdateAccount
        {
            get { return _lastUpdateAccount; }
            set { _lastUpdateAccount = value; }
        }

        private bool _isLive;
        [DataMember]
        public bool IsLive
        {
            get { return _isLive; }
            set { _isLive = value; }
        }

        private bool _isAvailable;
        [DataMember]
        public bool IsAvailable
        {
            get { return _isAvailable; }
            set { _isAvailable = value; }
        }


        public enum MarketType : byte
        {
            G1X2,
            FH1X2,
            GHDP,
            FHHDP,
            GOU,
            FHOU
        }

    }

    public enum MarketType : byte
    {
        M1X2,
        MOU,
        MHDP
    }
}
